package com.opdar.athena.web.senders;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;

/**
 * Created by shiju on 2017/8/16.
 */
public class KafkaSender implements ISender<Boolean> {
    @Autowired
    private KafkaTemplate<String, byte[]> kafkaTemplate;

    @Override
    public Boolean send(String key, byte[] value) {
        ListenableFuture<SendResult<String, byte[]>> future = kafkaTemplate.sendDefault(key, value);
        try {
            SendResult<String, byte[]> result = future.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public Boolean  send(String topic, String key, byte[] value) {
        ListenableFuture<SendResult<String, byte[]>> future = kafkaTemplate.send(topic, key, value);
        try {
            SendResult<String, byte[]> result = future.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return true;
    }
}
